<template>
  <el-dialog
    :title="title"
    :visible.sync="open"
    width="600px"
    append-to-body
    :close-on-click-modal="false"
    @close="closeModal"
  >
    <el-form ref="form" :model="form" :show-message="false" label-width="150px">
      <el-form-item label="分组名称" prop="groupName" required>
        <el-input
          v-model="form.groupName"
          placeholder="请输入分组名称"
          :maxlength="15"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="排序" prop="sort" required>
        <el-input-number
          v-model="form.sort"
          controls-position="right"
          placeholder="数字越小越排前"
          :step="1"
          step-strictly
          :min="1"
          :max="999"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="分组说明" prop="remark">
        <el-input
          type="textarea"
          :rows="3"
          placeholder="请输入分组说明"
          v-model="form.remark"
        >
        </el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="open = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addAcupointGroup, updateAcupointGroup } from "@/api/saas/tcm.js";
import { deepClone } from '@/utils';

export default {
  data() {
    return {
      title: "新建分组",
      open: false,
      form: {
        groupId: undefined,
        groupName: undefined,
        sort: undefined,
        remark: undefined
      },
    };
  },
  methods: {
    openModal(row) {
      if(row && row.groupId){
        this.title = "编辑分组";
        this.form = deepClone(row);
      }
      this.open = true;
    },
    closeModal() {
      this.$refs["form"].resetFields();
      Object.assign(this.$data, this.$options.data()); // 存在自定义校验规则不能重置data
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if(valid){
          if (this.form.groupId) {
            updateAcupointGroup(this.form).then(res => {
              this.$message.success("修改成功");
              this.$emit("refresh");
              this.closeModal();
            });
          } else {
            addAcupointGroup(this.form).then(res => {
              this.$message.success("新增成功");
              this.$emit("refresh");
              this.closeModal();
            })
          }
        }
      });
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
